use crate::pool;
use rbatis::{crud, impl_select_page, Page, rbdc, sql::{self, PageRequest}};
use rbdc::datetime::DateTime;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TbAppLog {
    #[serde(rename = "type")]
    pub log_type: Option<u32>,
    pub ip: Option<String>,
    pub req: Option<String>,
    pub rsp: Option<String>,
    pub create_at: Option<DateTime>,
    pub cost: Option<String>,
}

crud!(TbAppLog {});
impl_select_page!(TbAppLog{select_page(query: &crate::handlers::applog::Get) =>
    "`where 1=1`
    if query.log_type!=null:
        ` and type=#{query.log_type}`
    if query.ip!=null && query.ip!='':
        ` and ip=#{query.ip}`
    if query.begin_time!=null:
        ` and create_at>=#{query.begin_time}`
    if query.end_time!=null:
        ` and create_at<=#{query.end_time}`
    if query.text!=null && query.text!='':
        ` and text like #{'%'+query.text+'%'}`
    ` order by create_at desc`"
});

impl TbAppLog {
    pub async fn get_applogs(
        page: &crate::common::QueryPage,
        query: &crate::handlers::applog::Get,
    ) -> Result<Page<TbAppLog>, rbdc::Error> {
        TbAppLog::select_page(pool!(), &PageRequest::from(page), query).await
    }
}
